package util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.apache.commons.codec.binary.Base64;
import org.codehaus.jackson.JsonNode;

import play.Play;
import play.libs.F.Option;
import play.libs.F.Promise;
import play.libs.Json;
import play.libs.WS;
import play.libs.WS.Response;
import play.libs.WS.WSRequestHolder;

public class OAuthUtil {
	
	public static String getGoogleAuthUrl(long youId,Option<String> callback) {
		try {
			String endpoint = Play.application().configuration().getString("google.api.endpoint.auth");
			String clientId = Play.application().configuration().getString("google.client.id");
			String redirect = Play.application().configuration().getString("google.client.redirect");
			String scope = Play.application().configuration().getString("google.api.gmail.scope"); 
					
			StringBuffer sb = new StringBuffer();
			sb.append(endpoint);
			sb.append('?');
			sb.append("response_type=code");
			sb.append("&client_id=");
			sb.append(URLEncoder.encode(clientId, "UTF-8"));
			sb.append("&redirect_uri=");
			sb.append(URLEncoder.encode(redirect, "UTF-8"));
			sb.append("&scope=");
			sb.append(URLEncoder.encode(scope,"UTF-8"));
			sb.append("&state=");		// TODO fix csrf
			sb.append(URLEncoder.encode(youId+"$$"+callback.getOrElse(""),"UTF-8"));
			sb.append("&access_type=offline");
			sb.append("&approval_prompt=auto");
			return sb.toString();
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
	}
	
	public static Promise<Response> getGoogleTokenRequest(String code) {
//		try {
			String endpoint = Play.application().configuration()
					.getString("google.api.endpoint.token");
			String clientId = Play.application().configuration()
					.getString("google.client.id");
			String clientSecret = Play.application().configuration()
					.getString("google.client.secret");
			String redirect = Play.application().configuration()
					.getString("google.client.redirect");

			StringBuffer sb = new StringBuffer();
			sb.append("grant_type=authorization_code");
			sb.append("&client_id=");
			sb.append(/*URLEncoder.encode(clientId, "UTF-8")*/clientId);
			sb.append("&redirect_uri=");
			sb.append(/*URLEncoder.encode(redirect, "UTF-8")*/redirect);
			sb.append("&client_secret=");
			sb.append(/*URLEncoder.encode(clientSecret, "UTF-8")*/clientSecret);
			sb.append("&code=");
			sb.append(/*URLEncoder.encode(code, "UTF-8")*/code);

			return WS.url(endpoint)
					.setContentType("application/x-www-form-urlencoded")
					.post(sb.toString());

		/*} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
		*/
	}
	
	public static Promise<Response> revokeToken(String token) {
		String endpoint = Play.application().configuration()
				.getString("google.api.endpoint.revoke");
		
		return WS.url(endpoint).setQueryParameter("token", token).get();
	}

	public static JsonNode decodeBase64Json(String encodedIdToken) {
		try {
			byte[] decoded = Base64.decodeBase64(encodedIdToken.getBytes("UTF8"));
			String decodedIdToken = new String(decoded, "UTF8");
//			String decodedIdToken = new String(decoded).trim();
			System.out.println(decodedIdToken);
			JsonNode idTokenPayload = Json.parse(decodedIdToken);
			return idTokenPayload;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}
}
